import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_syt/datas/category_info_model.dart';
import 'package:flutter_syt/datas/user_info.dart';
import 'package:flutter_syt/domain/widgets/cell_item.dart';
import 'package:flutter_syt/domain/widgets/flow_item.dart';
import 'package:flutter_syt/domain/widgets/grid_nav.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/theme/font_styles.dart';
import 'package:flutter_syt/theme/theme_color.dart';
import 'package:flutter_syt/utils/global_date_time_format.dart';
import 'package:flutter_syt/utils/screen_adapter.dart';
import 'package:provider/provider.dart';
import 'package:yyy_route_annotation/yyy_route_annotation.dart';

class HomeCell extends StatelessWidget {
  final List<FlowItemEntity> data;

  const HomeCell(this.data, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Consumer<UserInfo>(builder: (ctx, info, child) {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            padding: EdgeInsets.only(left: 15, top: 24),
            child: Text(
              S.of(context).latest_news,
              style: TextStyle(
                color: Color(0xff000000),
                fontSize: 20,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
          // Container(
          //   alignment: Alignment.topLeft,
          //   height: 230,
          //   width: MediaQuery.of(context).size.width,
          //   margin: EdgeInsets.only(top: 6, left: 0, right: 0),
          //   child: ListView.separated(
          //     scrollDirection: Axis.horizontal,
          //     padding: EdgeInsets.symmetric(horizontal: 16),
          //     itemCount: data.length,
          //     separatorBuilder: (BuildContext context, int index) =>
          //         const VerticalDivider(
          //       width: 16,
          //       color: Colors.transparent,
          //     ),
          //     itemBuilder: (BuildContext context, int index) {
          //       var each = data[index];
          //       return GestureDetector(
          //         child: CellItem(each),
          //         onTap: () => RouteManager.push(context, each.linkUrl),
          //       );
          //     },
          //   ),
          // )
        ],
      );
    });
  }
}

class HomeGridItem extends StatelessWidget {
  final CategoryInfoModel data;

  const HomeGridItem(this.data, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 68.w,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(4)),
        // color: data.color,
      ),
      child: Stack(
        children: [
          Positioned(
              child: Container(
                width: 60,
                height: 60,
                child: OverflowBox(
                  alignment: Alignment.bottomRight,
                  maxWidth: 100,
                  maxHeight: 100,
                  child: Container(
                    height: 60,
                    width: 60,
                    alignment: Alignment.bottomRight,
                    child: Image.network(
                      data.icon ?? '',
                      fit: BoxFit.contain,
                    ),
                  ),
                ),
                // width: (ScreenAdapter.screenW()-30-16)/3,
              ),
              bottom: 0,
              right: 0),
          Positioned(
            child: Container(
              decoration: BoxDecoration(
                  // gradient: LinearGradient(
                  //   begin: Alignment.centerLeft,
                  //   end: Alignment.centerRight,
                  //   stops: [0.5, 1],
                  //   // colors: [data.color, data.color.withAlpha(0)],
                  // ),
                  ),
            ),
            top: 0,
            bottom: 0,
            left: 4,
            right: 0,
          ),
          Positioned(
            child: Text(
              data.name ?? '',
              style: TextStyle(
                color: Colors.black,
                fontFamily: 'PingFang SC',
                fontSize: 14,
              ),
            ),
            top: 12,
            left: 10,
          )
        ],
      ),
    );
  }
}

class HomeJingangGridItem extends StatelessWidget {
  final GridNavItemEntity data;

  const HomeJingangGridItem(this.data, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 64,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(4)),
        color: data.color,
      ),
      child: Stack(
        children: [
          Positioned(
              child: Container(
                width: 60,
                height: 60,
                child: OverflowBox(
                  alignment: Alignment.bottomRight,
                  maxWidth: 100,
                  maxHeight: 100,
                  child: Container(
                    height: 60,
                    width: 60,
                    alignment: Alignment.bottomRight,
                    child: Image.network(
                      data.iconImage,
                      fit: BoxFit.contain,
                    ),
                  ),
                ),
                // width: (ScreenAdapter.screenW()-30-16)/3,
              ),
              bottom: 0,
              right: 0),
          Positioned(
            child: Container(
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  begin: Alignment.centerLeft,
                  end: Alignment.centerRight,
                  stops: [0.5, 1],
                  colors: [data.color, data.color.withAlpha(0)],
                ),
              ),
            ),
            top: 0,
            bottom: 0,
            left: 4,
            right: 0,
          ),
          Positioned(
            child: Text(
              data.text,
              maxLines: 2,
              style: TextStyle(
                color: Colors.black,
                fontFamily: 'PingFang SC',
                fontSize: 14,
              ),
            ),
            top: 12,
            left: 10,
            right: 5,
          )
        ],
      ),
    );
  }
}

class Encyclopedias {
  late int id;
  late String name;
  late String icon;
  late String linkUrl;
  late Color color = Colors.white;
  late int head_occupy = 0;
  Encyclopedias(Map<String, dynamic> json) {
    try {
      id = json["id"] as int;
    } catch (e) {
      id = 0;
    }
    try {
      head_occupy = json["head_occupy"] as int;
    } catch (e) {
      head_occupy = 0;
    }
    this.name = json["name"] as String;
    this.icon = json["icon"] as String;
    this.linkUrl = '${json["path"]}';
  }

  Map<String, dynamic> get json {
    var result = Map<String, dynamic>();
    result["id"] = id;
    result["name"] = this.name;
    result["icon"] = this.icon;
    result["path"] = this.linkUrl;
    result["head_occupy"] = this.head_occupy;
    return result;
  }

  /// 百科使用的背景颜色。
  static List<Color> colorList = [
    Color(0xffEAF7FC),
    Color(0xffEAF7FC),
    Color(0xffFEF6E7),
    Color(0xffFFF0EA),
    Color(0xffFFF0EA),
    Color(0xffFEF6E7),
  ];
}
